package com.syni.mdd.sjb.community.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syni.mdd.sjb.common.component.mybatis.community.entity.SjbQywxAdmin;
import com.syni.mdd.sjb.common.component.mybatis.community.entity.SjbQywxAdminRoleRelation;
import com.syni.mdd.sjb.common.component.mybatis.community.mapper.SjbQywxAdminRoleRelationMapper;
import com.syni.mdd.sjb.community.base.constants.Constants;
import com.syni.mdd.sjb.community.base.service.SjbQywxAdminRoleRelationService;
import com.syni.mdd.sjb.community.base.service.SjbQywxAdminService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 管理后台角色关系表 服务实现类
 * </p>
 *
 * @author lcc
 * @since 2021-02-23
 */
@Service
public class SjbQywxAdminRoleRelationServiceImpl extends ServiceImpl<SjbQywxAdminRoleRelationMapper, SjbQywxAdminRoleRelation> implements SjbQywxAdminRoleRelationService {
    @Autowired
    SjbQywxAdminRoleRelationMapper sjbQywxAdminRoleRelationMapper;

    @Autowired
    SjbQywxAdminService sjbQywxAdminService;

    @Override
    public SjbQywxAdminRoleRelation getQywxAdminId(Integer qywxAdminId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(SjbQywxAdminRoleRelation.QYWX_ADMIN_Id,qywxAdminId);
        List<SjbQywxAdminRoleRelation> sjbQywxAdminRoleRelations = sjbQywxAdminRoleRelationMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(sjbQywxAdminRoleRelations)) {
            return sjbQywxAdminRoleRelations.get(0);
        }
        return null;
    }

    @Override
    public void syncAdminRoleRelationInfomation(Integer qywxAdminId, Integer roleId) {
        SjbQywxAdminRoleRelation sjbQywxAdminRoleRelation = getQywxAdminId(qywxAdminId);
        if(sjbQywxAdminRoleRelation == null){
            sjbQywxAdminRoleRelation = new SjbQywxAdminRoleRelation();
        }
        sjbQywxAdminRoleRelation.setRoleId(roleId);
        sjbQywxAdminRoleRelation.setQywxAdminId(qywxAdminId);
        this.saveOrUpdate(sjbQywxAdminRoleRelation);
    }



    @Override
    public synchronized void addCompanyAdminOrPlatformMan(String corpId,Integer qywxAdminId) {
        SjbQywxAdmin sjbQywxAdmin = sjbQywxAdminService.getBySuperAdminByCorpId(corpId);
        if(sjbQywxAdmin != null && sjbQywxAdmin.getId().equals(qywxAdminId)){
            return;
        }
        Integer roleId = Constants.QYWX_ADMIN_ROLE_PM;
        if(sjbQywxAdmin == null){
            roleId = Constants.QYWX_ADMIN_ROLE_CREATE;
        }
        syncAdminRoleRelationInfomation(qywxAdminId,roleId);
    }

    @Override
    public Map<Integer, SjbQywxAdminRoleRelation> getQywxAdminIds(Set ids) {
        Map<Integer, SjbQywxAdminRoleRelation> sjbQywxAdminRoleRelationMap = new HashMap<>();
        if(ids.size() > 0){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.in(SjbQywxAdminRoleRelation.QYWX_ADMIN_Id,ids);
            List<SjbQywxAdminRoleRelation> sjbQywxAdminRoleRelations = sjbQywxAdminRoleRelationMapper.selectList(queryWrapper);
            if(CollectionUtils.isNotEmpty(sjbQywxAdminRoleRelations)){
                for(SjbQywxAdminRoleRelation sjbQywxAdminRoleRelation : sjbQywxAdminRoleRelations){
                    sjbQywxAdminRoleRelationMap.put(sjbQywxAdminRoleRelation.getQywxAdminId(),sjbQywxAdminRoleRelation);
                }
            }
        }
        return sjbQywxAdminRoleRelationMap;
    }

    @Override
    public void deleteAdminRolelations(Set<Integer> adminIds) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in(SjbQywxAdminRoleRelation.QYWX_ADMIN_Id,adminIds);
        List<SjbQywxAdminRoleRelation> sjbQywxAdminRoleRelations = sjbQywxAdminRoleRelationMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(sjbQywxAdminRoleRelations)){
            this.removeByIds(sjbQywxAdminRoleRelations.stream().map(SjbQywxAdminRoleRelation::getId).collect(Collectors.toSet()));
        }
    }
}
